package com.bc.ui;

import java.util.Timer;
import java.util.TimerTask;

import com.bc.utils.Constants;
import com.bc.utils.SettingPrefs;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.IBinder;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

public class BtcService extends Service {
	private BtcCacheOperator mBtcCacheOperator;
	public static final String ACTION_NETWORK_BTCNEWS = "com.bc.ACTION_NETWORK_BTCNEWS";
	private LocalBroadcastManager mBroadcastManager;
	public static boolean started = false;
	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		started = true;
		mBtcCacheOperator = BtcCacheOperator.getInstance();
		mBroadcastManager = LocalBroadcastManager.getInstance(this);
		mBroadcastManager.registerReceiver(mRefreshReceiver, new IntentFilter(ACTION_NETWORK_BTCNEWS));
		mHandler = new Handler();
	}
	
	private Handler mHandler;
	private BroadcastReceiver mRefreshReceiver = new BroadcastReceiver() {
		
		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent.getAction().equals(BtcService.ACTION_NETWORK_BTCNEWS)) {
				mBtcCacheOperator.resetEnabledBtcNews();
				mBtcCacheOperator.resetBtcCategories();
				mBtcCacheOperator.resetServiceCatalogHashMap();
				
				mBtcCacheOperator.initEnabledBtcNews(BtcService.this);
				mBtcCacheOperator.clearTimerHashMap();
				sendRefreshLoadingBroadcast();
				mBtcCacheOperator.updateAllServiceData(BtcService.this);
			}
		}
	};
	private Timer mTimer;
	private long refreshDex;
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		mBtcCacheOperator.resetEnabledBtcNews();
		mBtcCacheOperator.resetBtcCategories();
		mBtcCacheOperator.resetServiceCatalogHashMap();
		
		mBtcCacheOperator.initEnabledBtcNews(this);
		mBtcCacheOperator.clearTimerHashMap();
//		sendRefreshLoadingBroadcast();
		mBtcCacheOperator.updateAllServiceData(BtcService.this);
		refreshDex = 1000 *SettingPrefs.getInstance(BtcService.this).getIntFromPrefs(SettingPrefs.REFRESH_TIME, 10);
		if (mTimer == null) {
			mTimer = new Timer();
		}
		
		mTimer.schedule(new TimerTask() {
			
			@Override
			public void run() {
				mHandler.post(new Runnable() {
					
					@Override
					public void run() {
						sendRefreshLoadingBroadcast();
						mBtcCacheOperator.updateAllServiceData(BtcService.this);
						refreshDex = 1000 *SettingPrefs.getInstance(BtcService.this).getIntFromPrefs(SettingPrefs.REFRESH_TIME, 10);
						Log.d(getClass().getSimpleName(), "refreshDex: " + refreshDex);
					}
				});
			}
		}, 10000, refreshDex);
		
		return Service.START_STICKY;
	}
	
	public void sendRefreshLoadingBroadcast() {
		Intent intent = new Intent(Constants.ACTION_SHOW_LOADING_REFRESH);
		LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
	}
	
	@Override
	public void onDestroy() {
		mBtcCacheOperator.resetEnabledBtcNews();
		mBtcCacheOperator.resetBtcCategories();
		mBtcCacheOperator.resetServiceCatalogHashMap();
		mBtcCacheOperator.clearTimerHashMap();
		started = false;
		super.onDestroy();
	}
	
}
